/**
 * JAVASCRIPT FOR FOREVERPO PAGE
 * @author aduc272
 */


function initialize() {
	alert('abc');
    //$('textLyricsChords').autosize();  

}

function addSong() {
	//alert(document.getElementById("txtArtist").value);
}

function cancel(){
	alert("cancel");
}

/**
 * functions repeated
 */

function getControl(idControl) {
	return document.getElementById(idControl);
}

function getXMLObject() {
	var xmlhttp;
	
	// code for IE7+, Firefox, Chrome, Opera, Safari
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else 	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return xmlhttp;
}

/**
 * functions for showing songs
 */

function hideDash() {
	var dash = getControl("dash");
	dash.innerHTML = dash.innerHTML.replace(/-/g, "&nbsp;");	
	dash.parentNode.onclick = showDash;
	
	var lyricsChords = getControl("textLyricsChords");
	lyricsChords.innerHTML = lyricsChords.innerHTML.replace(/-/g, "&nbsp;");
}

function showDash() {
	var dash = getControl("dash");
	dash.innerHTML = dash.innerHTML.replace(/&nbsp;/g, "-");
	dash.parentNode.onclick = hideDash;
	
	var lyricsChords = getControl("textLyricsChords");
	lyricsChords.innerHTML = lyricsChords.innerHTML.replace(/&nbsp;/g, "-");
}


/**
 * functions for changing tone
 */

function getToneById(xml, toneId) {	
	var tone = null;	
	var xpath = "tone[id='" + toneId + "']";
	tone = xml.find(xpath);
	return $(tone);
}

function getToneFromChord(chord) {
	var tone = null;
	if(chord.indexOf("b") != -1 || chord.indexOf("#") != -1) {
		//Chords: Bb, F#m, Ebm...
		tone = chord.substr(0,2) ;		
	} else {
		//chords: C, D, Am,...
		tone = chord[0];
	}	
	return tone;
}

function getChordFromTone(chord, newTone) {
	var newChord = null;
	if(chord.length == 1) {
		//chords: C, D, G,...
		newChord = newTone;
	} else if(chord.indexOf("b") != -1 || chord.indexOf("#") != -1) {
		//chords: Bb, F#m, Ebm...
		newChord = chord.replace(chord.substr(0, 2), newTone);
	} else {
		//chords: Am, F7, A7, Dm... 
		newChord = newTone + chord[chord.length-1];
	}
	
	return newChord;
}

function next() {
	var xmlhttp = getXMLObject();
	//xmlhttp.open("GET","http://192.168.1.23:800/aduc272/w3c/xml/tones.xml?sid=" + Math.random(),false);
	xmlhttp.open("GET","http://aductt.free.fr/w3c/xml/tones.xml?sid=" + Math.random(),false);
	xmlhttp.send();
	xmlDoc = xmlhttp.responseXML;
	
	$xml = $(xmlDoc);
	var i = 0;
	$(".lyrics_chords").children().each(function(){
		var chord = $(this);
				
		var curToneId = getToneFromChord(chord.text());
		
		var xpath = "tone[name='" + curToneId + "']";
		var curTone = $xml.find(xpath);
		var nextToneId = $(curTone).attr("next");
		var nextTone = getToneById($xml, nextToneId);			
		
		//change to next tone
		var nextChord = getChordFromTone(chord.text(), nextTone.attr("name"));
		chord.text(nextChord);		
		
		return;
    });	
}

function prev() {
	var xmlhttp = getXMLObject();
	//xmlhttp.open("GET","http://192.168.1.23:800/aduc272/w3c/xml/tones.xml?sid=" + Math.random(),false);
	xmlhttp.open("GET","http://aductt.free.fr/w3c/xml/tones.xml?sid=" + Math.random(),false);
	xmlhttp.send();
	xmlDoc = xmlhttp.responseXML;
	
	$xml = $(xmlDoc);
	var i = 0;
	$(".lyrics_chords").children().each(function(){
		var chord = $(this);
				
		var curToneId = getToneFromChord(chord.text());
		
		var xpath = "tone[name='" + curToneId + "']";
		var curTone = $xml.find(xpath);
		var prevToneId = $(curTone).attr("prev");
		var prevTone = getToneById($xml, prevToneId);			
		
		//change to next tone
		var prevChord = getChordFromTone(chord.text(), prevTone.attr("name"));
		chord.text(prevChord);		
		
		return;
    });	
}




